<?php
/**
 * Created by PhpStorm.
 * User: chenzhitao
 * Date: 2018/4/21
 * Time: 下午5:12
 */

namespace App\Admin\Controllers;

use App\Busi\Traits\CompanyTrait;
use App\Models\Company;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use Encore\Admin\Controllers\ModelForm;
use App\Http\Controllers\Controller;
use App\Models\CompanyStaff;
class CompanyStaffController extends Controller
{
    use ModelForm,CompanyTrait;
    public function index()
    {
        return Admin::content(function (Content $content) {
            $content->header('公司精英团队列表');
            $content->description('');
            $content->body($this->grid());
        });
    }

    public function create()
    {
        return Admin::content(function (Content $content) {
            $content->header('新增公司精英');
            $content->description('');
            $content->body($this->form());
        });
    }
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {
            $content->header('编辑');
            $content->description('');
            $content->body($this->form('edit')->edit($id));
        });
    }

    protected function grid() {
        return Admin::grid(CompanyStaff::class, function (Grid $grid) {
            $obj = $this;
            $grid->id('ID')->sortable();
            $grid->company_id("所属公司")->display(function ($company_id) use($obj){
                return $obj->getCompanyNameByID($company_id);
            });

            $grid->name('姓名');
            $grid->phone('电话');
            $grid->avatar("头像")->display(function ($avatar){
                return "<img src='".config('admin.upload.host')."{$avatar}' width='100' height='100'/>";
            });
            $grid->experience("从业经历");
            $grid->is_hidden("是否隐藏")->display(function ($is_hidden){
                if( $is_hidden==1 ) return '隐藏';
                return '显示';
            });
            $grid->flag('是否生效')->display(function($flag){
                if( $flag==1 ) return '有效';
                return '无效';
            });
            $grid->created_at('创建时间');
            $grid->filter(function ($filter) {
            });

            $grid->actions(function (Grid\Displayers\Actions $actions) {

            });

            $grid->tools(function (Grid\Tools $tools) {
                $tools->batch(function (Grid\Tools\BatchActions $actions) {
//                    $actions->disableDelete();
                });
            });
        });
    }

    protected function form($mode='')
    {
        return Admin::form(CompanyStaff::class, function (Form $form) use($mode){
            if($mode=='edit'){
                $form->display('id','ID');
                $form->display('created_at', '创建时间');
                $form->display('updated_at', '更新时间');
            }
            $form->select('company_id', '所属公司')->options(Company::pluck("name","id"));

            $form->text('name','姓名');
            $form->text('phone','电话');
            $form->text('experience','从业经历');
            $form->image('avatar', '头像')->uniqueName()->removable();
            $form->radio('is_hidden', '是否隐藏')->options([0=>'否', 1=>'是']);
            $form->radio('flag', '是否生效')->options([0=>'否', 1=>'是']);
        });
    }
}